package com.force.module.webserver.util;

import java.util.HashSet;
import java.util.Set;


import cn.hutool.extra.spring.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Component
public class CacheUtils {
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 获取CacheManager
     *
     * @return
     */
    public static CacheManager getCacheManager() {
        return SpringUtil.getBean(CacheManager.class);
    }

    /**
     * 根据cacheName从CacheManager中获取cache
     *
     * @param cacheName
     * @return
     */
    public static Cache getCache(String cacheName) {
        return getCacheManager().getCache(cacheName);
    }

    /**
     * 获取缓存的所有key值(由于springcache不支持获取所有key,只能根据cache类型来单独获取)
     *
     * @param cacheName
     * @return
     */
    public  Set<String> getkeys(String cacheName) {
        Cache cache = getCacheManager().getCache(cacheName);
        return getCachekeys(cache);
    }
    public Set<String> getCachekeys(Cache cache) {
        Set<String> keyset = new HashSet<>();
        Set<Object> keysets = redisTemplate.keys(cache.getName() + "*");
        for (Object s : keysets) {
            keyset.add(StringUtils.replace(s.toString(), cache.getName() + ":", ""));
        }
        return keyset;
    }

    public  boolean hasKey(String cacheName, String key) {
        return !ObjectUtils.isEmpty(get(cacheName, key));
    }


    /**
     * 获取数据
     *
     * @param cacheName
     * @param key
     * @return
     */
    public  Object  get(String cacheName, String key) {
        Cache cache = getCacheManager().getCache(cacheName);
        if(null!=cache){
            return cache.get(key);
        }
        return null;
    }
    /**
     * 根据cacheName,key缓存数据
     *
     * @param cacheName
     * @param key
     * @param value
     * @param <T>
     */
    public  <T> void put(String cacheName, String key, T value) {
        getCacheManager().getCache(cacheName).put(key,value);
    }

    /**
     * 如果没有则进行缓存,根据cacheName,key缓存数据
     *
     * @param cacheName
     * @param key
     * @param value
     * @param <T>
     */
    public  <T> void putIfAbsent(String cacheName, String key, T value) {
        getCacheManager().getCache(cacheName).putIfAbsent(key,value);
    }

    /**
     * 根据类型获取数据
     *
     * @param cacheName
     * @param key
     * @param type
     * @param <T>
     * @return
     */
    public  <T> T get(String cacheName, String key, @Nullable Class<T> type) {
        return (T) getCacheManager().getCache(cacheName).get(key);
    }

    /**
     * 移除缓存数据
     *
     * @param cacheName
     * @param key
     */
    public static void remove(String cacheName, String key) {
        getCacheManager().getCache(cacheName).evict(key);
    }

    /**
     * 如果存在则移除缓存数据
     *
     * @param cacheName
     * @param key
     * @return
     */
    public static boolean removeIfPresent(String cacheName, String key) {
       return getCacheManager().getCache(cacheName).evictIfPresent(key);
    }

    /**
     * 清除缓存名称为cacheName的所有缓存数据
     *
     * @param cacheName
     */
    public static void clear(String cacheName) {
        getCacheManager().getCache(cacheName).clear();
    }
}
